//index.js
//获取应用实例
const app = getApp()
import req from '../../utils/Request.js';
import appReq from '../../utils/AppRequest.js';
import areaList from "./city.js"
const QQMapWX = require('../../utils/qqmap-wx-jssdk');
let qqmapsdk;

Page({
  data: {
    Height: 0,
    latitude: 36.652118,
    longitude: 117.120266,
    markers: [],
    show: false,
    newsPicture: "", //开屏图
 
    cityShow: false,
    searchShow: false,
    areaList: areaList,
    listQuery: {
      cityId: "",
      cityName: "济南市",
      houseName: ""
    },
    cityName: "济南市"
  },

  onReady: function (e) {
    // 操作 wx.createMapContext 获取 map 上下文
    this.mapCtx = wx.createMapContext('myMap')
  },

  onLoad: function () {
    this.getImg();
    qqmapsdk = new QQMapWX({
      key: 'B7HBZ-QEZRP-U2CDK-L2O4A-3DSU3-GEBGL'
  });
    wx.showShareMenu({
      withShareTicket: true
    })
    var _this = this;
    //获取屏幕高度，设置地图满屏
    wx.getSystemInfo({
      success: function (res) {
        //设置map高度，根据当前设备宽高满屏显示
        _this.setData({
          Height: res.windowHeight
        })
      }
    })
    wx.getLocation({
      type: 'wgs84',
      success(res) {
         _this.setData({
          latitude: res.latitude,
          longitude: res.longitude,
        })
        qqmapsdk.reverseGeocoder({
          location: {
            latitude: res.latitude,
            longitude: res.longitude,
          },
          success: function (res) {
            _this.data.listQuery.cityName = res.result.address_component.city;
            _this.data.listQuery.cityId = res.result.ad_info.city_code.substring(3);
            wx.setStorageSync("cityId",_this.data.listQuery.cityId)
            wx.setStorageSync("cityName",_this.data.listQuery.cityName)
            _this.setData({
              cityName: res.result.address_component.city,
              listQuery: _this.data.listQuery,
            })
            _this.getList()
          },
          fail: function (res) {
            console.log(res);
          }, 
        })
      }
    })
  },

  onShow: function () {
    if (wx.getStorageSync("cityId")) {
      this.data.listQuery.cityId = wx.getStorageSync("cityId");
      this.setData({
        cityName: wx.getStorageSync("cityName"),
        listQuery: this.data.listQuery,
      })
      this.getList()
    }
  },

  onLaunch: function () {
    this.setData({
      show: true,
    })
  },

  moveTolocation: function () {
    this.mapCtx.moveToLocation();
  },

  close: function () {
    this.setData({
      show: false,
    })
  },

  getList: function (val) {
    let markers = this.data.markers;
    req.get('/index.do', this.data.listQuery).then(res => {
      if (res.code == 1) {
        let arr = res.data
        console.log(arr)

        for (let i in arr) {
          arr[i].id = arr[i].keyId;
          arr[i].latitude = arr[i].lat;
          arr[i].longitude = arr[i].lon;
          arr[i].width = 20;
          arr[i].height = 18;
          arr[i].iconPath = "../../images/home_house.png";
          arr[i].callout = {
            content: arr[i].houseName + '\n约' + arr[i].perFlatPrice + '/㎡',
            bgColor: arr[i].buildCategory === "商用房" ? '#409EFF' : '#ffa800',
            fontSize: 12,
            borderWidth: 0,
            borderColor: "#CCC",
            padding: 8,
            display: "ALWAYS",
            textAlign: "center",
            borderRadius: 28,
            color: '#ffe9bf'
          };
        }

        this.setData({
          markers: arr,
        })

        if (val && arr.length > 0) {
          this.setData({
            latitude: arr[0].lat,
            longitude: arr[0].lon,
          })
        }
      
        wx.hideLoading();
      } else {
        app.showErrorMessage(res, '加载失败');
        wx.hideLoading();
        return;
      }
    }).catch(res => {
      wx.hideLoading();
      wx.showToast({
        title: '网络异常',
        icon: 'none'
      });
      console.log("err catch");
    });
  },

  goPage() {
    if (this.data.newsPicture.newsLink && this.data.newsPicture.newsLink !== '0') {
      wx.navigateTo({
        url: '../detail/index?keyId=' + this.data.newsPicture.newsLink
      })
    } else {
      wx.navigateTo({
        url: '../newDetail/index?keyId=' + this.data.newsPicture.keyId
      })
    }
  },

  getImg: function () {
    req.get('/index/pageList.do', {
      newsType: "indexPicture",
      page: 1,
      pageSize: 100000
    }).then(res => {
      if (res.code == 1) {
        if (res.data.content.length > 0) {
          this.setData({
            show: true,
            newsPicture: res.data.content[0]
          })
        }
        console.log(this.data.newsPicture)
        wx.hideLoading();
      } else {
        app.showErrorMessage(res, '加载失败');
        wx.hideLoading();
        return;
      }
    }).catch(res => {
      wx.hideLoading();
      wx.showToast({
        title: '网络异常',
        icon: 'none'
      });
      console.log(res);
    });
  },

  goDetail: function (res) {
    wx.navigateTo({
      url: '../detail/index?keyId=' + res.markerId
    })
  },

  openCity() {
    this.setData({ cityShow: true });
  },

  handleClose() {
    this.setData({ cityShow: false });
  },

  getCity(val) {
    this.data.listQuery.cityId = val.detail.values[1].code;
    wx.setStorageSync("cityId",val.detail.values[1].code)
    wx.setStorageSync("cityName",val.detail.values[1].name)
    this.setData({
      cityName: val.detail.values[1].name,
      listQuery: this.data.listQuery,
      cityShow: false
    })
    this.getList()
  },
  openSearch() {
    this.setData({
      searchShow: !this.data.searchShow
    })
  },

  getInputCity(e) {
    this.data.listQuery.houseName = e.detail.value
    this.setData({listQuery: this.data.listQuery})
    this.getList("search")
  }
})